package com.wjjun.base.binding.adapter

import androidx.databinding.BindingAdapter
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.wjjun.base.ui.listener.IRefreshViewListener

/**
 *
 * author : wangjunjun
 * date   : 2022/6/9
 * desc   :
 */

class SmartRefreshLayoutAdapter {
    companion object {

        @JvmStatic
        @BindingAdapter(value = ["onRefresh"])
        fun smartRefreshLayoutOnRefresh(smartRefreshLayout: SmartRefreshLayout, listener: IRefreshViewListener?) {
            if (listener == null) {
                return
            }
            listener.initRefresh(smartRefreshLayout)
            smartRefreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
                override fun onLoadMore(refreshLayout: RefreshLayout) {
                    listener.onLoadMore(refreshLayout)
                }

                override fun onRefresh(refreshLayout: RefreshLayout) {
                    listener.onRefresh(refreshLayout)
                }
            })
        }

        @JvmStatic
        @BindingAdapter(value = ["noMoreData"])
        fun setNoMoreData(refreshEnable: SmartRefreshLayout, noMoreData: Boolean) {
            refreshEnable.setNoMoreData(noMoreData)
        }

        @JvmStatic
        @BindingAdapter(value = ["enableRefresh"])
        fun setEnableRefresh(refreshEnable: SmartRefreshLayout, enableRefresh: Boolean) {
            refreshEnable.setEnableRefresh(enableRefresh)
        }

        @JvmStatic
        @BindingAdapter(value = ["enableLoadMore"])
        fun setEnableLoadMore(refreshEnable: SmartRefreshLayout, enableLoadMore: Boolean) {
            refreshEnable.setEnableLoadMore(enableLoadMore)
        }
    }
}